<template>
  <div>
    <div class="container">
      <div class="row">
        <div class="login">
          <div class="logo">
            <router-link to="/"></router-link>
          </div>
          <div class="main">
            <form action="">
              <div class="reglogin">
                <ul class="tab">
                  <li
                    id="acountLogin"
                    :class="{ 'z-crt': currentindex == index }"
                    v-for="(item, index) in loginTitle"
                    :key="index"
                    @click="currentindex = index"
                  >
                    {{ item }}
                  </li>
                </ul>
                <div class="tab_content">
                  <div class="tab_item" :class="{ 'z-crt': currentindex == 0 }">
                    <div class="f-item">
                      <input
                        type="text"
                        class="u-ipt"
                        name="account"
                        v-model="loginData.phone"
                        placeholder="科沃斯ID/手机号"
                        autocomplete="new-password"
                      />
                      <div class="login error" v-if="flag">
                        用户名或密码不正确
                      </div>
                    </div>
                    <div class="f-item">
                      <input
                        type="password"
                        class="u-ipt"
                        name="password"
                        v-model="loginData.password"
                        placeholder="登录密码"
                        autocomplete="new-password"
                        @keydown.enter="loginIn"
                      />
                    </div>
                  </div>
                  <div class="tab_item" :class="{ 'z-crt': currentindex == 1 }">
                    <div class="f-item">
                      <input
                        type="text"
                        class="u-ipt"
                        name="loginMobile"
                        placeholder="请输入11位手机号"
                      />
                    </div>
                    <div class="f-item row">
                      <div class="col-md-7">
                        <input
                          type="text"
                          class="u-ipt"
                          name="mobileCaptcha"
                          placeholder="请输入手机验证码"
                        />
                      </div>
                      <div class="col-md-4">
                        <button
                          type="button"
                          id="mobileCaptchaBtn"
                          class="u-btn u-btn-c3"
                          disabled=""
                        >
                          发送验证码
                        </button>
                      </div>
                    </div>
                    <div class="f-item">
                      <p style="color: #999">
                        温馨提示：未注册的手机号，登录时将自动注册，且代表您已同意<a
                          href="https://www.ecovacs.cn/policy/agreement"
                          target="_blank"
                          style="color: #0000ff"
                          >《用户注册协议》</a
                        >。
                      </p>
                    </div>
                  </div>
                </div>
                <div class="f-item button">
                  <input
                    type="button"
                    class="u-btn J-submit"
                    disabled=""
                    value="登录"
                    :class="{ red: able }"
                    :disabled="!able"
                    @click="loginIn"
                  />
                </div>
                <div class="f-item f-bottom">
                  <router-link to="/register">立即注册</router-link>
                  | <router-link to="/forget">忘记密码</router-link>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <div class="login-ad">
      <a href="#/product/2"></a>
    </div>
    <!-- 底部 -->
    <Footer></Footer>
    <!-- 侧边咨询客服按钮 -->
    <consult-serve></consult-serve>
  </div>
</template>

<script>
import { mapActions, mapMutations } from "vuex";
import { login } from "../assets/js/request.js";
import Footer from "../components/Footer.vue";
import ConsultServe from "../components/ConsultServe.vue";
export default {
  data() {
    return {
      loginTitle: ["账号密码登录", "手机快捷登录"],
      currentindex: 0,
      loginData: {
        phone: "",
        password: "",
      },
      flag: false,
    };
  },
  components: {
    Footer,
    ConsultServe,
  },
  computed: {
    able() {
      return this.loginData.phone && this.loginData.password;
    },
  },
  methods: {
    ...mapActions("cart", ["getCartprod"]),
    ...mapMutations("cart", ["getUserId"]),
    loginIn() {
      login(this.loginData)
        .then((res) => {
          console.log(res);
          this.getUserId(res.id);
          sessionStorage.setItem("token", res.token);
          sessionStorage.setItem("phone", res.phone);
          this.getCartprod();
          this.flag = true;
          if (this.$route.path == this.isPath || this.isPath == "/register") {
            this.$router.push("/");
          } else {
            this.$router.push(this.isPath);
          }
        })
        .catch(() => {
          this.flag = true;
        });
    },
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.isPath = from.path;
    });
  },
};
</script>

<style lang="scss">
.container {
  width: 1220px;
  margin: 0 auto;
  .login {
    position: relative;

    .logo {
      height: 97px;
      line-height: 97px;
      text-align: left;

      a {
        display: inline-block;
        width: 250px;
        height: 50px;
        margin-top: 30px;

        &::before {
          content: "";
          display: inline-block;
          width: 240px;
          height: 50px;
          background: url(../assets/image/login/default.png) center no-repeat;
          background-size: contain;
        }
      }
    }

    .main {
      position: absolute;
      z-index: 2;
      top: 129px;
      right: 17px;
      background-color: #fff;

      .reglogin {
        position: relative;
        padding: 30px;
        width: 400px;
        height: 526px;
        overflow: hidden;

        .tab {
          display: flex;
          position: absolute;
          // top: 20px;
          top: 0;
          left: 0;
          width: 100%;
          height: 50px;
          padding: 0;

          li {
            width: 50%;
            height: 50px;
            line-height: 50px;
            border-bottom: 1px solid #e0e0e0;
            font-size: 18px;
            text-align: center;
            list-style: none;
            cursor: pointer;
          }

          li.z-crt {
            color: #ee0a3b;
            border-color: #ee0a3b;
            cursor: default;
          }
        }

        .tab_content {
          margin-top: 64px;

          .tab_item {
            display: none;

            .f-item {
              position: relative;
              padding-bottom: 25px;
              text-align: justify;

              .u-ipt {
                color: #888;
                font-size: 14px;
                width: 100%;
                height: 50px;
                padding: 10px;
                border: 1px solid #e0e0e0;
              }
              .login.error {
                width: 100%;
                height: 20px;
                position: absolute;
                bottom: 2px;
                left: 0;
              }
              .login.error::before {
                display: inline-block;
                vertical-align: middle;
                content: "";
                width: 18px;
                height: 20px;
                background: url(../assets/image/register/icons-201703.png) -66px
                  0 no-repeat;
              }

              .col-md-7 {
                display: inline-block;
                padding: 0;
                // padding-left: 15px;
                width: 58%;
              }

              .col-md-4 {
                display: inline-block;
                padding: 0;
                margin-left: 9%;
                width: 33%;

                .u-btn {
                  padding: 0;
                  width: 100%;
                  height: 50px;
                  line-height: 50px;
                  font-weight: 400;
                  color: #888;
                  cursor: default;
                }
              }
              p {
                margin: 0;
              }
            }
          }

          .tab_item.z-crt {
            display: block;
          }
        }

        .f-item.button {
          padding-bottom: 25px;

          input {
            padding: 0;
            width: 100%;
            height: 50px;
            line-height: 50px;
            font-weight: 400;
            font-size: 14px;
            text-align: center;
            outline: 0;
            color: #888;
            cursor: default;
          }
          input.red {
            background-color: #ee0a3b;
            color: #fff;
            border: 1px solid transparent;
          }
        }

        .f-item.f-bottom {
          text-align: center;

          a {
            color: #888;
            text-decoration: none;
            font-size: 14px;

            &:hover {
              color: #ee0a3b;
            }
          }
        }

        .f-oauth {
          position: absolute;
          left: 30px;
          right: 30px;
          bottom: 30px;
          padding-top: 20px;
          border-top: 1px solid #e0e0e0;

          .title {
            position: absolute;
            background-color: #fff;
            font-size: 14px;
            color: #888;
            width: 112px;
            height: 22px;
            top: -11px;
            left: 124px;
            text-align: center;
          }

          .items {
            text-align: center;
            height: 34px;

            i + i {
              margin-left: 30px;
            }

            i {
              &:first-of-type {
                &:hover {
                  color: #0288d1;
                }
              }

              &:nth-of-type(2) {
                &:hover {
                  color: #d32f2f;
                }
              }

              &:nth-of-type(3) {
                &:hover {
                  color: #00aaee;
                }
              }

              &:nth-of-type(4) {
                &:hover {
                  color: #04b205;
                }
              }
            }

            .iconfont {
              width: 29px;
              height: 29px;
              font-size: 29px;
              color: #747474;
            }
          }
        }
      }
    }
  }
}
.login-ad {
  height: 590px;

  a {
    display: block;
    width: 100%;
    height: 100%;
    background: url(../assets/image/login/ia_700000009.jpg) center no-repeat;
  }
}
</style>